/**
 * @author caslyn
 */
$(document).ready(function(){

	$("#dialog_edit_class").dialog({
		bgiframe: true,
		autoOpen: false,
		width: 490,
		modal: true,
		buttons: {
			'Save': function() {
				temp = $('#class_description').val();
				temp = temp.replace(/\n/g,"<br>");
				$.post("json.php", { 
					"task": "addNewClass",
					"lecturer": $('#lecturer').val(), 
					"description": temp,
					"weekday": $('#weekday').val(),
					"fromhour": $('#fromhour').val(),
					"fromminutes": $('#fromminutes').val(),
					"fromampm": $('#fromampm').val(),
					"tohour": $('#tohour').val(),
					"tominutes": $('#tominutes').val(),
					"toampm": $('#toampm').val(),
					"location": $('#location').val(),
					"subjectid": $('#subjectid').val(),
					"semesterid": $('#semesterid').val() } , 
					function(data){
						if(data > 0 ){
							$('#classlist').append('<tr id="trclass'+data+'">' +
								"<td class='aligntop ui-widget-content ui-corner-all'>" + $('#lecturer').children(':selected').text() + "</td>" +
								"<td class='aligntop ui-widget-content ui-corner-all'>" + temp + "</td>" + 
								"<td class='aligntop ui-widget-content ui-corner-all' style='text-align: right' width='200'>" + 
								$('#weekday').children(':selected').text() + ', ' +
								$('#fromhour').val() + ':' +
								$('#fromminutes').children(':selected').text() +
								$('#fromampm').children(':selected').text() + ' to ' +
								$('#tohour').val() + ':' +
								$('#tominutes').children(':selected').text() +
								$('#toampm').children(':selected').text() + ' </td>' +
								"<td class='aligntop ui-widget-content ui-corner-all'>" + $('#location').val() + "</td>" + 
								"<td class='aligntop ui-widget-content ui-corner-all' width='80'>" +
								WriteActionButton("View", "viewClass('" + data + "')" ) +
								' ' +
								WriteActionButton("Delete", "deleteClass('" + data + "')") +
								'</td></tr>' );
						} else {
							alert("Server error: " + data);
						}
						$("#dialog_edit_class").dialog('close');
					}
				);
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			$('#subject_name').val('');
			$('#subject_description').val('');
		}
	});

// -------------------------------------------------------------------------------------
	$('a.ui-corner-all').hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	).mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
			$(this).removeClass("ui-state-active");
	});

// -------------------------------------------------------------------------------------

	$('#editClassDesc').click(function(){
		temp = $('#spnClassDesc').html();
		$('#class_description').val(temp.replace(/<br>/g,"\n").replace(/<br\/>/g,"\n"));
//		$('#weekday').text($('#spnWeekday').html());
//		$('#newsubjectname').val($('#subjectname').text());
		$('#dialog_edit_class').dialog('open');
	});

	// Load lecturer
	$('#lecturer').empty();
	$.getJSON("json.php",{
		"task": "getPresidentCandidates",
		"id": $('#schoolid').val()
	}, function(data){
		$.each(data.candidates, function(i,item){ 
            $('#lecturer').append('<option value="' + item.id + '">' + item.name + '</option>');
		}); 
	});

	// Load timetable value
	for(i=1;i<13;i++){
		$('#fromhour').append('<option value="' + i + '">' + i + '</option>');
		$('#tohour').append('<option value="' + i + '">' + i + '</option>');
	}
	for(i=0;i<46;i+=15){
		$('#fromminutes').append('<option value="' + (i==0?"00":i) + '">' + (i==0?"00":i) + '</option>');
		$('#tominutes').append('<option value="' + (i==0?"00":i) + '">' + (i==0?"00":i) + '</option>');
	}
	$('#fromampm').append('<option value="am">AM</option>');
	$('#fromampm').append('<option value="pm">PM</option>');
	$('#toampm').append('<option value="am">AM</option>');
	$('#toampm').append('<option value="pm">PM</option>');
	
	$('#weekday').append('<option value="1">Monday</option>');
	$('#weekday').append('<option value="2">Tuesday</option>');
	$('#weekday').append('<option value="3">Wednesday</option>');
	$('#weekday').append('<option value="4">Thursday</option>');
	$('#weekday').append('<option value="5">Friday</option>');
	$('#weekday').append('<option value="6">Saturday</option>');
	$('#weekday').append('<option value="7">Sunday</option>');
	$('#weekday').val($('#hidweekdayid').val());

	$('#linkHome').click(function(){
		$('#hiddentask').val('default');
		$('#hiddenid').val($('#schoolid').val());
		$('#hiddenform').submit();

	});

	$('#linkFaculty').click(function(){
		$('#hiddentask').val('faculty');
		$('#hiddenid').val($('#facultyid').val());
		$('#hiddenform').submit();

	});

	$('#linkDepartment').click(function(){
		$('#hiddentask').val('department');
		$('#hiddenid').val($('#departmentid').val());
		$('#hiddenform').submit();

	});

	$('#linkSubjct').click(function(){
		$('#hiddentask').val('subject');
		$('#hiddenid').val($('#subjectid').val());
		$('#hiddenform').submit();

	});

});